package source.events.action;

import source.model.technology.UnitTechnology;
import source.model.type.ActionResult;
import source.model.*;
import source.model.type.GameObjectType;

public class BaseNewTurn implements Action {
	
	private ObjectID baseID;
	
	public BaseNewTurn(ObjectID oid)
	{
		this.baseID = oid;
	}
	public ActionResult execute(DataHandler dh)
	{
		GameObject b = dh.objectQuery(baseID);
		Base base;
		if ( b.objectType() != GameObjectType.BASE ) {
			dh.returnObject( b );
			return ActionResult.IMPOSSIBLE;
		}
		base = (Base)b;

		Player thatGuy = dh.playerQuery(base);
		Planet p = (Planet)(dh.objectQuery(base.planetID()));
		thatGuy.incrementOre(p.extractOre(base.getExtractionRate()));
		thatGuy.incrementTotalResearch(p.getManPower());
		
		int upkeep = base.getUpkeep();
		base.modHealth(-thatGuy.incrementEnergyUsed(upkeep));
		
		//heal units in the healing queue
		base.healUnits();
		
		if(thatGuy.finishedResearching()){
			UnitTechnology tech=(UnitTechnology)thatGuy.getResearching();
			thatGuy.addNextTech(tech);
			thatGuy.researchNewTechnology(null);
			tech.teach(thatGuy);
			LearnNewTechnologyAction lnta=new LearnNewTechnologyAction(thatGuy.getID());
			lnta.execute(dh);
		}
		
		else
			thatGuy.research();
		
		dh.returnObject(base);

		return ActionResult.SUCCESS;		
	}

}
